package com.ymatou.iapi.livemanage.testcase;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livemanage.service.GetCountryGroupCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.AppCountry;
import com.ymttest.database.mongo.CountryInfoBean;
import com.ymttest.database.mongo.CountryInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.AppCountryWapper;

public class Ts_GetCountryGroup {
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播Admin-国家查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetCountryGroupCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCountryGroup() {
		Logger.start(true, "直播Admin-国家查询");
		
		try{
			//发送国家查询请求
			GetCountryGroupCall getCountryGroupCall = new GetCountryGroupCall();
			getCountryGroupCall.callService();
			JSONObject countryReqObj = new JSONObject(getCountryGroupCall.getReturnData());
			JSONArray countryGroupAry = countryReqObj.getJSONObject("Data").getJSONArray("GroupList");
			
			
			//检查报文返回值
			Logger.verifyEquals(200,countryReqObj.getInt("Code"), "响应报文-返回码");
			
			//计算请求返回国家个数
			int actTotalCount = 0;
			for (int i=0;i<countryGroupAry.length();i++){
				actTotalCount = actTotalCount + (countryGroupAry.getJSONObject(i).getJSONArray("children").length()-1);
			}
			
			
			//检查SQL和Mongo Country值和个数
			
			//SQL 链接
			AppCountryWapper appCountryWapper = new AppCountryWapper();
			List<AppCountry> appCountryList = appCountryWapper.selectAll();
			
			Logger.verifyEquals(appCountryList.size(),actTotalCount, "数据库SQL-APPCountry 国家个数检查");
			
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
			//Mongo 链接
				CountryInfoWapper countryInfoWapper = new CountryInfoWapper();
				List<CountryInfoBean> countryInfoList = countryInfoWapper.getAll();
				
				Logger.verifyEquals(countryInfoList.size(),actTotalCount, "数据库Mongo-CountryInfo 国家个数检查");
				
				
				for (int i=0;i<countryGroupAry.length();i++){
					JSONArray groupChildren = countryGroupAry.getJSONObject(i).getJSONArray("children");
					for (int j=0;j<groupChildren.length();j++){
						JSONObject childJsonObject = groupChildren.getJSONObject(j);
						if (childJsonObject.getInt("CountryId") == -1){
							continue;
						}
						//SQL 
						AppCountry appCountry = sqlSearchById(appCountryList,childJsonObject.getInt("CountryId"));
						Logger.verifyEquals(appCountry.getIcountryid(), childJsonObject.getInt("CountryId"), "数据库SQL-APPCountry 国家ID");
						Logger.verifyEquals(appCountry.getScountrynamezh(), childJsonObject.getString("CountryName"), "数据库SQL-APPCountry 国家中文名");
						Logger.verifyEquals(appCountry.getScountryname(), childJsonObject.getString("CountryNameEN"), "数据库SQL-APPCountry 国家英文名");
						
						//Mongo
						CountryInfoBean countryInfoBean = mongoSearchById(countryInfoList,childJsonObject.getInt("CountryId"));
						Logger.verifyEquals(countryInfoBean.getCountryId(), childJsonObject.getInt("CountryId"), "数据库Mongo-CountryInfo 国家ID");
						Logger.verifyEquals(countryInfoBean.getCountryNameZh(), childJsonObject.getString("CountryName"), "数据库Mongo-CountryInfo 国家中文名");
						Logger.verifyEquals(countryInfoBean.getCountryName(), childJsonObject.getString("CountryNameEN"), "数据库Mongo-CountryInfo 国家英文名");
					}
				}
			}
			
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
		
		
	
	
	private AppCountry sqlSearchById(List<AppCountry> appCountryList,int countryId){
		for (AppCountry appCountry:appCountryList){
			if (appCountry.getIcountryid()==countryId){
				return appCountry;
			}
		}
		return null;
	}
	
	private CountryInfoBean mongoSearchById(List<CountryInfoBean> countryInfoList,int countryId){
		for (CountryInfoBean countryInfoBean: countryInfoList){
			if (countryInfoBean.getCountryId() == countryId){
				return countryInfoBean;
			}
		}
		return null;
	}
	
	
	
	
	
	
}
